package com.igoal.service.paging;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.igoal.dto.UserGoalDTO;
import com.igoal.entity.DataStatus;
import com.igoal.entity.GoalStatusEnum;
import com.igoal.service.GoalManager;
import com.igoal.service.UserGoalManager;
import com.igoal.utils.ConvertUtils;

@Service("pagingServiceDWR")
public class PagingServiceImpl implements PagingService {
	
	private int USER_GOAL = 1;
	private int GOAL_ENTRY = 2;
	private int HOW_I_DONE = 3;
	
	private GoalManager goalManager;
//	private UserManagerExt userManagerExt;
	private UserGoalManager userGoalManager;
	
	@Autowired
	public void setGoalManager(GoalManager goalManager) {
		this.goalManager = goalManager;
	}

//	@Autowired
//	public void setUserManagerExt(UserManagerExt userManagerExt) {
//		this.userManagerExt = userManagerExt;
//	}
	
	@Autowired
	public void setUserGoalManager(UserGoalManager userGoalManager) {
		this.userGoalManager = userGoalManager;
	}


	@SuppressWarnings("unchecked")
	public Page getPage(int itemType, Map keyValues, String orderProperty,
			String lastOrderPropertyValue, boolean before) {
		if(itemType != USER_GOAL && itemType != GOAL_ENTRY && itemType != HOW_I_DONE)
			return null;
		
		Page page = null;
		
		if(USER_GOAL == itemType){
			page = getUserGoalPage(keyValues, orderProperty, lastOrderPropertyValue, before);
		}else if(GOAL_ENTRY == itemType){
			
		}else if(HOW_I_DONE == itemType){
			
		}
		
		return page;
	}
	
	@SuppressWarnings("unchecked")
	public Page getUserGoalPage(Map keyValues, String orderProperty, String lastOrderPropertyValue, boolean before){				
		if(keyValues.containsKey("userId")){
			Long userId = Long.parseLong(keyValues.get("userId").toString());
			keyValues.remove("userId");
			keyValues.put("pk.user.id", userId);
		}
		
		if(keyValues.containsKey("status")){
			String status = (String) keyValues.get("status");
			DataStatus dataStatus = DataStatus.valueOf(status);
			keyValues.remove("status");
			keyValues.put("status", dataStatus);
		}
		
		if(keyValues.containsKey("goalStatus")){
			String status = (String) keyValues.get("goalStatus");
			GoalStatusEnum goalStatus = GoalStatusEnum.valueOf(status);
			keyValues.remove("goalStatus");
			keyValues.put("goalStatus", goalStatus);
		}
		
		Date date = null;//TODO: remove hardcode
		if(lastOrderPropertyValue != null){
			Long time = ConvertUtils.toLong(lastOrderPropertyValue, -1L);
			if(time != -1L){
				date = new Date(time);
			}
		}
		
		List<UserGoalDTO> goals = userGoalManager.getUserGoalsByPaging(keyValues, orderProperty, date, before);
		if(goals != null){
			Page page = new Page();
			List<PageItem> pageItems = new ArrayList<PageItem>(goals);
			page.setPageItems(pageItems);
			page.setLastOrderPropertyValue(lastOrderPropertyValue);
			page.setOrderProperty(orderProperty);
			return page;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public Page getPage(int itemType, Map keyValues, String orderProperty,
			String lastOrderPropertyValue) {
		return getPage(itemType, keyValues, orderProperty, lastOrderPropertyValue, false);
	}
}
